/**
 * 
 */
package com.bitBusy.manageyourriches.database.managers;

import java.util.Calendar;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.text.format.Time;
import android.view.View;
import android.widget.TextView;

import com.bitBusy.manageyourriches.utilityClasses.checkers.NullChecker;
import com.bitBusy.manageyourriches.utilityClasses.dialogboxes.DialogBoxPrompts;
import com.bitBusy.manageyourriches.utilityClasses.display.DisplayMessages;
import com.bitBusy.manageyourriches.utilityClasses.pickers.DateChangeListener;
import com.bitBusy.manageyourriches.utilityClasses.pickers.DatePickerFragment;
import com.bitBusy.manageyourriches.utilityClasses.pickers.TimeChangeListener;
import com.bitBusy.manageyourriches.utilityClasses.pickers.TimePickerFragment;

/**
 * Class with functionality to set date and time
 * @author SumaHarsha
 *
 */
public class DateTimeManager{
	
	private static final String ZEROPADDING = "%02d";


	/** date component splitter*/
	public static final String DATESPLITTER = "/";
	
	/**Date splitter for DB*/
	public static final String DBDATESPLITTER = "-";

	/**date time separator*/
	public static final String DATETIMESPLITTER= " ";

	/**time splitter*/
	public static final String TIMESPLITTER = ":";
	
	/** Days*/
	public static final String[] DAYS = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
	
	private static final String PM = "PM";
	
	 private static final String AM = "AM";  

	
	/**Method to get today's date 
	 * @return return string of today s date
	 */
	public static String getCurrentDate()
	{
				return new StringBuilder().append(getCurrentMonth()).append(DATESPLITTER).append(getCurrentDay()).append
						(DATESPLITTER).append(getCurrentYear()).toString();
   	}
	
	/**Method to get current time
	 * @return string of current time
	 */
	public static String getCurrentTime()
	{
	    return new StringBuilder().append(String.valueOf(getCurrentHour())).append
		    		 (TIMESPLITTER).append(String.valueOf(getCurrentMin())).append(TIMESPLITTER).append(String.valueOf(getCurrentSec())).toString();
	}
	
	

	/**Method to change date
	 * @param context
	 * @param the fragment
	 * @param the view object
	 */
	public static void changeDate(Context the_context, FragmentActivity the_fragment, View the_view)
	{
		if (!NullChecker.checkForNullValues(new Object[]{the_context, the_fragment, the_view}))
		{
			DatePickerFragment newFragment = new DatePickerFragment();
			newFragment.addListener((DateChangeListener) the_context);
			if(!newFragment.setOnObject((TextView) the_view))
			{
				DisplayMessages.displayInternalError(the_context, DialogBoxPrompts.INVALIDVIEWDIALOGALERT.getPrompt());
			}
			
			newFragment.show(the_fragment.getFragmentManager(), "datePicker");
		}
	}
	
	/**Method to change the time
	 * @param the context
	  * @param the fragment
	 * @param the view object
	 */
	public static void changeTime(Context the_context, FragmentActivity the_fragment, View the_view)	
	{
		TimePickerFragment newFragment = new TimePickerFragment();
		newFragment.addListener((TimeChangeListener) the_context);
		if(!newFragment.setOnObject((TextView) the_view))
		{
				DisplayMessages.displayInternalError(the_context, DialogBoxPrompts.INVALIDVIEWDIALOGALERT.getPrompt());
		}
		
	    newFragment.show(the_fragment.getFragmentManager(), "timePicker");

	}
	/**Method to get current month
	 * @return string value of current month
	 */
	public static int getCurrentMonth()
	{
		Time now = new Time(Time.getCurrentTimezone());
		now.setToNow();
		int month = now.month + 1;
		return month;
	
	}
	
	/**Method to get current day
	 * @return int value of current day
	 */
	public static int getCurrentDay()
	{
		Time now = new Time(Time.getCurrentTimezone());
		now.setToNow();
		return now.monthDay;
	}

	/**Method to get current year
	 * @return current year
	 */
	public static int getCurrentYear()
	{
		Time now = new Time(Time.getCurrentTimezone());
		now.setToNow();
		return now.year;
	}


	/**
	 * Method to verify if time is after current time
	 * @param time
	 * @return boolean indicating if it is
	 */
	public static boolean isAfterNow(String the_time) {
		if (the_time != null)
		{
			String[] timeComponents = the_time.split(TIMESPLITTER);
			if (timeComponents != null)
			{
				int hour = Integer.parseInt(timeComponents[0]);
				int min = Integer.parseInt(timeComponents[1]);
				//int sec = Integer.parseInt(timeComponents[2]);
				
				if (hour > getCurrentHour() || (hour == getCurrentHour() && min > getCurrentMin()))
				{
					return true;
				}
			}
		}
		return false;
	}
	
	/**Method to get current hour
	 * @return int hour
	 */
	public static int getCurrentHour()
	{
		Time now = new Time(Time.getCurrentTimezone());
		now.setToNow();
		return now.hour;
	}
	
	/**Method to get current min
	 * @return int min
	 */
	public static int getCurrentMin()
	{
		Time now = new Time(Time.getCurrentTimezone());
		now.setToNow();
		return now.minute;
	}
	
	/**Method to get current sec
	 * @return int sec
	 */
	public static int getCurrentSec()
	{
		Time now = new Time(Time.getCurrentTimezone());
		now.setToNow();
		return now.second;
	}
	

	/** Method to parse date into the yyyy-mm-dd format
	 * @param the date
	 * @return formatted date
	 */
	public static int parseForDBDate(String the_date)
	{
		if (the_date != null)
		{
			String[] dateComponents = the_date.split(DateTimeManager.DATESPLITTER);
			if (dateComponents.length == 3)
			{
				String month = dateComponents[0];
				month = (month.length() == 1)?correctDateLength(month):month;
				String day = dateComponents[1];
				day = (day.length() == 1)?correctDateLength(day):day;

				String year = dateComponents[2];
	     	
			
				//return new StringBuilder().append(year).append(DBDATESPLITTER).append(month).append(DBDATESPLITTER).append(day)
				//.toString();
				return Integer.parseInt
						(new StringBuilder().append(year).append(month).append(day).toString());
				//.toString(); )
			}
		}
		return -1;
	}
	
	/** method to correct date length (make it two digits standard)
	 * @param string to correct
	 * @return corrected string
	 */
	private static String correctDateLength(String the_date)
	{
		if (the_date != null)
		{
			StringBuffer correctedBuffer = new StringBuffer("0");
			correctedBuffer.append(the_date);
			return correctedBuffer.toString();
		}
		return null;
	}
	
	/** Method to parse date into the yyyy-mm-dd format
	 * @param the date
	 * @return formatted date
	 */
	/*public static String parseForDBDate(String the_date)
	{
		if (the_date != null)
		{
			String[] dateComponents = the_date.split(DateTimeManager.DATESPLITTER);
			if (dateComponents.length == 3)
			{
				String month = dateComponents[0];
				String day = dateComponents[1];
				String year = dateComponents[2];
	     	
			
				return new StringBuilder().append(year).append(DBDATESPLITTER).append(month).append(DBDATESPLITTER).append(day)
				.toString();
			}
		}
		return null;
	}
	*/
	

	/** Method to parse date into the mm/dd/yyyy format
	 * @param the date
	 * @return formatted date
	 */
	public static String parseForUserDate(int the_date)
	{
		StringBuffer returnString = new StringBuffer("");
		int counter = 0;
		int component = 0;
		StringBuffer temp = new StringBuffer("");
		if (the_date != 0)
		{
			while(counter < 4)
			{
				component = the_date % 10;
				the_date /= 10;
				temp.insert(0, component);
				if (counter == 1 || counter == 3)
				{
					returnString.insert(0, temp);
					temp.delete(0, temp.length());
					if (counter == 1)
					{ 
						returnString.insert(0, DateTimeManager.DATESPLITTER);
						returnString.append(DateTimeManager.DATESPLITTER);
					}
				}
				counter++;
			}
			returnString.append(String.valueOf(the_date));
		}
		return returnString.toString();
	}
	
	/** Getdatetime
	 * @param date
	 * @param time
	 * @return datetime
	 */
	public static String getDateTime(int the_date, String the_time)
	{
		if (the_time != null)
		{
			return the_date + DATETIMESPLITTER + the_time;
		}
		return null;
	}
	
	/** Get date
	 * @param date time
	 * @return date
	 */
	public static int getDate(String the_dateTime)
	{
		if (the_dateTime != null)
		{
			String[] dateTime = the_dateTime.split(DATETIMESPLITTER);
			if (dateTime.length > 0)
			{
				return Integer.parseInt(dateTime[0]);
			}
					
		}
		return -1;
	}
	
	/** Get time
	 * @param date time
	 * @return time
	 */
	public static String getTime(String the_dateTime)
	{
		if (the_dateTime != null)
		{
			String[] dateTime = the_dateTime.split(DATETIMESPLITTER);
			if (dateTime.length > 1)
			{
				return dateTime[1];
			}
					
		}
		return null;
	}
	
	/**Method to get previous month, given a date
	 * @param the date
	 * @return the previous month string value
	 */
	public static String getPreviousMonth()
	{
		int currMonth = getCurrentMonth();
		return String.valueOf((currMonth - 1));
		
	}

	/**
	 * @param date
	 * @return day
	 */
	public static String getDay(int the_date) {
		String date = parseForUserDate(the_date);
		String[] components = date.split(DATESPLITTER);
		Calendar cal = Calendar.getInstance();
		cal.set(Integer.parseInt(components[2]), Integer.parseInt(components[0]), Integer.parseInt(components[1]));
		return DAYS[cal.get(Calendar.DAY_OF_WEEK) - 1];
			}
	
	/**
	 * @param hour
	 * @param minute
	 * @return 
	 */
	public static String getTime(int the_hour, int the_minute) {
		int hour = the_hour>12?the_hour-12:the_hour;
		return new StringBuilder().append(String.format(ZEROPADDING,hour)).append(DateTimeManager.TIMESPLITTER).append
				(String.format(ZEROPADDING, the_minute)).append(the_hour>12?PM:AM).toString();
	}

	/**
	 * @param day
	 * @param month
	 * @param year
	 * @return
	 */
	public static String getDate(int the_day, int the_month, int the_year) {
		return new StringBuilder().append(String.valueOf(the_month)).append(DATESPLITTER).append(String.valueOf(the_day)).
				append(DATESPLITTER).append(String.valueOf(the_year)).toString();
	}



	
	
	}
